package web.java.login;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class loginServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//对html页面传入参数进行解码
		req.setCharacterEncoding("utf-8");
		//加载登录时间进主界面
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		String time=sdf.format(date);
		//将time时间存放在request域对象中
		//req.setAttribute("time", time);
		//req.getRequestDispatcher("/index").forward(req, resp);;
		//接收用户输入的用户名和密码
		String name=req.getParameter("name");
		String password=req.getParameter("password");
		//创建session，使用session传递用户数据
		HttpSession session=req.getSession();
		session.setAttribute("name", name);
		//判断
		if("cainter".equals(name)&&"admin".equals(password)){
			//用户登录成功，请求重定向
			resp.sendRedirect(req.getContextPath()+"/index");
		}else{
			//用户登录失败，请求重定向
			resp.sendRedirect(req.getContextPath()+"/failure.html");
		}
	}
}
